#!/bin/bash
# Install LAMP For RHEL/CentOS
# Version: 2013.12.30
CWD=$(pwd)
. ./functions/soft_version.sh
. ./functions/mysql.sh
. ./functions/system.sh
. ./functions/logs.sh
. ./functions/apache.sh
. ./functions/php4apache.sh
. ./functions/php_modules.sh

system_check
welcome_lamp
check_current_user

echo -e "\033[31;49;1mWelcome to LAMP-2013-Final installation - by Luxiaok\033[39;49;0m"
echo ""

echo -ne "Press any key to start install \033[32;49;1mLAMP\033[39;49;0m ... "
char=`get_char`
echo
echo

logs "Info" "Checking System"

# 时间校对，默认不校对，注释
#NTP_CN

#系统优化，默认不优化，注释
#Optimization

#### Check Packages ####
logs "Info" "Check packages"

rpm -q gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glibc-common glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel libtool libtool-libs libevent-devel libevent openldap openldap-devel nss_ldap openldap-clients openldap-servers libtool-ltdl libtool-ltdl-devel bison gettext zip gd gd-devel cmake patch \
| grep "not installed" >> $logs_file

##########  Install pkgs  ##########
logs "Info" "Install System Packages"

yum -y remove httpd nginx php mysql-server mysql mysql-libs php-mysql

yum -y install \
gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glibc-common glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel libtool libtool-libs libevent-devel libevent openldap openldap-devel nss_ldap openldap-clients openldap-servers libtool-ltdl libtool-ltdl-devel bison gettext zip gd gd-devel pcre pcre-devel crontabs cmake patch sendmail

cd $CWD

############# Install Apache #########################
logs "Info" "Install Apache"
Apache_Install

############### Install MySQL #################
logs "Info" "Install MySQL"
# 调用MySQL安装函数
cd $CWD
MySQL_Install

############ Ready for install PHP  ##############
logs "Info" "Ready For Install PHP"

cd $CWD
########## Install libiconv ########
logs "Info" "Install libiconv"
tar -zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local
make && make install
cd ..

#### Install libmcrypt #####
logs "Info" "Install libcrypt"
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make && make install
cd ..

### Install mhash ###
logs "Info" "Install mhash"
cd $CWD
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make && make install
cd ..

logs "Info" "Create soft link for libmcrypt"
if [ "$bit" = "x64" ] ; then
ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/libiconv.so.2
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
else
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
fi
 
### Install mcrypt ###
logs "Info" "Install mcrypt"
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure
make && make install
cd ..

logs "Info" "Create sotf links for libs(libsmysqlclient,libjpeg,libpng)"
if [ "$bit" = "x64" ] ; then
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so 
ln -s /usr/lib64/libldap.so /usr/lib/libldap.so 
ln -s /usr/lib64/libpng.so /usr/lib/libpng.so
else
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
fi

/sbin/ldconfig

############ Install PHP #############
logs "Info" "Install PHP 5.3.28 For Apache"
php4apache_install
cd $CWD

########## PHP Modules Install ########
php_modules_install

##########################
mkdir -p /var/www/localhost
cp $CWD/conf/lamp/httpd.vhost.conf /usr/local/apache/conf/conf.d/localhost.conf
cp $CWD/conf/tz.php /var/www/localhost/
cp $CWD/conf/info.php /var/www/localhost/
cp $CWD/conf/index.php /var/www/localhost/
cp $CWD/conf/robots.txt /var/www/localhost/
sed -i 's/LNMP/LAMP/g' /var/www/localhost/index.php
sed -i 's/Nginx/Apache/g' /var/www/localhost/index.php
chown www.www -R /var/www
rm -f /etc/httpd/httpd.conf.bak
mv /etc/httpd/httpd.conf /etc/httpd/httpd.conf.org
cp $CWD/conf/lamp/httpd.conf /etc/httpd/

logs "Info" "Install phpMyAdmin"
cd $CWD
tar -jxf phpMyAdmin-$pmVer.tar.bz2
mv phpMyAdmin-$pmVer /var/www/localhost/phpmyadmin
chown www.www -R /var/www/localhost

# 在LAMP环境中，默认不开启ZendGuardLoader，仅针对于php-5.3.28版本
mv /usr/local/php/etc/php.d/zend.ini /usr/local/php/etc/php.d/zend.ini.stop
/etc/init.d/httpd start

logs "Info" "Install Complete"
# get localhost IP address
ip=`ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}' |grep -v "127.0.0.1" |head -n 1`

echo "===================================== Check install ==================================="
if [ -s /usr/local/apache ]; then
  echo "/usr/local/apache [found]"
  else
  echo "Error: /usr/local/apache not found!!!"
fi
if [ -s /usr/local/php ]; then
  echo "/usr/local/php [found]"
  else
  echo "Error: /usr/local/php not found!!!"
fi
if [ -s /usr/local/mysql ]; then
  echo "/usr/local/mysql [found]"
  else
  echo "Error: /usr/local/mysql not found!!!"
fi

echo "Congratulations!!"
echo "Linux + Apache + MySQL + PHP is sucessfully!"

echo "========================== Check Install ================================"
echo "                 Install LAMP 2013(Final)completed! enjoy it."
echo "========================================================================="
echo "        LAMP V2013 for CentOS/RadHat Linux Written by Luxiaok.com "
echo "========================================================================="
echo ""
echo "     For more information please visit http://cto.luxiaok.com/"
echo ""
echo " Test Page: http://$ip/index.php"
echo "   phpinfo: http://$ip/info.php"
echo "phpMyAdmin: http://$ip/phpmyadmin"
echo "   iProber: http://$ip/tz.php"
echo ""
echo "default MySQL root password is Null"
echo ""
echo "The path of LAMP directorys:"
echo " MySQL DIR:   /usr/local/mysql"
echo "   PHP DIR:   /usr/local/php"
echo "Apache DIR:   /usr/local/apache"
echo "   WEB DIR:   /var/www/"
echo " Vhost DIR:   /etc/httpd"
echo ""
echo "=========================================================================="

cat >> $CWD/tips.txt << EOF
Some information for LAMP:
 Test Page: http://$ip/index.php
   phpinfo: http://$ip/info.php
phpMyAdmin: http://$ip/phpmyadmin
   iProber: http://$ip/tz.php
    
Default MySQL root password is Null

The path of LAMP directorys:
MySQL DIR: /usr/local/mysql
Conf file: /usr/lcao/mysql/etc/my.cnf
PHP  DIR : /usr/local/php
Conf file: /usr/local/php/etc/php.ini
ApacheDIR: /usr/local/apache
Conf file: /usr/local/apache/conf/httpd.conf
  WEB DIR: /var/www/
Vhost DIR: /etc/httpd
EOF
netstat -ntlup